<?php

/**
 *    商品分享管理控制器
 *
 *    @author    Hyber
 *    @usage    none
 */
class VcategoryApp extends BackendApp
{
	var $_m_vcategory;

	function __construct()
	{
		$this->VcategoryApp();
	}

	function VcategoryApp()
	{
		parent::BackendApp();

		$this->_m_vcategory = &m('vcategory');
	}

	/**
	 *    商品分享索引
	 *
	 *    @author    Hyber
	 *    @return    void
	 */
	function index()
	{
		$search_options = array(
            'cate_name'   => Lang::get('key'),

		);
		/* 默认搜索的字段是店铺名 */
		$field = 'd_key';
		array_key_exists(@$_GET['field'], $search_options) && $field = @$_GET['field'];
		$conditions = $this->_get_query_conditions(array(array(
            'field' => $field,       //按用户名,店铺名,支付方式名称进行搜索
            'equal' => 'LIKE',
            'name'  => 'search_name',
		),
		));

		$page   =   $this->_get_page(10);    //获取分页信息
		$this->assign('filtered', $conditions? 1 : 0); //是否有查询条件
		$vcategorys = $this->_m_vcategory->find(array(
            'fields'   => '*',
            'conditions'    => '1=1 '.$conditions.'order by cate_id desc',
            'limit'   => $page['limit'],
            'count'   => true   //允许统计
		));
		$page['item_count'] = $this->_m_vcategory->getCount();   //获取统计的数据
		$this->import_resource(array('script' => 'inline_edit.js'));
		$this->_format_page($page);


		$this->assign('search_options', $search_options);
		$this->assign('page_info', $page);          //将分页信息传递给视图，用于形成分页条
		$this->assign('vcategorys', $vcategorys);
		$this->display('vcategory.index.html');
	}

	function add()
	{
		if (!IS_POST)
		{
			$this->import_resource('jquery.plugins/jquery.validate.js');
			$this->assign('vcategory', array('sort_order' => 255));
			$this->display('vcategory.form.html');
		}
		else
		{
			$data = array(
                'cate_name' => trim($_POST['cate_name']),
                'isnav'  => trim($_POST['isnav']),
                'sort_order'  => intval($_POST['sort_order']),
			);
		 if (!$this->_m_vcategory->unique(trim($data['cate_name'])))
		 {
		 	$this->show_warning('name_exist');
		 	return;
		 }
			if (!$id = $this->_m_vcategory->add($data))
			{
				$this->show_warning('添加失败');
				return;
			}
			$this->show_message('add_successed',
                'back_list',    'index.php?app=vcategory',
                'continue_add', 'index.php?app=vcategory&amp;act=add'
                );
		}
	}

	function edit()
	{
		$id = empty($_GET['id']) ? 0 : $_GET['id'];
		if (!$id)
		{
			$this->show_warning('no_such_vcategory');
			return;
		}
		$vcategory     = $this->_m_vcategory->find($id);
		$vcategory    =   current($vcategory);
		if (empty($vcategory))
		{
			$this->show_warning('no_such_vcategory');
			return;
		}
		if (!IS_POST)
		{
			$this->import_resource('jquery.plugins/jquery.validate.js');
			$this->assign('vcategory', $vcategory);
			$this->display('vcategory.form.html');
		}
		else
		{
			$data = array(
                'cate_name' => trim($_POST['cate_name']),
                'isnav'  => trim($_POST['isnav']),
                'sort_order'  => intval($_POST['sort_order']),
			);

		 if (!$this->_m_vcategory->unique(trim($data['cate_name']),$id))
		 {
		 	$this->show_warning('name_exist');
		 	return;
		 }
			$rows=$this->_m_vcategory->edit($id,$data);
			if ($this->_m_vcategory->has_error())
			{
				$this->show_warning('修改失败');
				return;
			}

			$this->show_message('edit_dictionary_successed',
                'back_list',     'index.php?app=vcategory',
                'edit_again', 'index.php?app=vcategory&amp;act=edit&amp;id=' . $id);
		}
	}

	function drop()
	{
		$dictionary_ids = isset($_GET['id']) ? trim($_GET['id']) : 0;
		if (!$dictionary_ids)
		{
			$this->show_warning('no_such_vcategory');

			return;
		}
		$dictionary_ids = explode(',', $dictionary_ids);//获取一个类似array(1, 2, 3)的数组
		foreach ($dictionary_ids as $dictionary_id)
		{
			$this->_m_vcategory->drop($dictionary_id);
		}
		$this->show_message('drop_dictionary_successed');
	}

	//异步修改数据
	function ajax_col()
	{
		$id     = empty($_GET['id']) ? 0 : intval($_GET['id']);
		$column = empty($_GET['column']) ? '' : trim($_GET['column']);
		$value  = isset($_GET['value']) ? trim($_GET['value']) : '';
		$data   = array();

		if (in_array($column ,array('isnav', 'sort_order')))
		{
			$data[$column] = $value;
			$this->_m_vcategory->edit($id, $data);
			if(!$this->_m_vcategory->has_error())
			{
				echo ecm_json_encode(true);
			}
		}
		else
		{
			return ;
		}
		return ;
	}

	function _get_dictionary_type()
	{
		return array(
            '0' => Lang::get('no'),
            '1'   => Lang::get('yes'),
        	'2'   => Lang::get('other'),
		);
	}

}